
//	Global jscript code.	2004.2.2 written by zhu
//	file purpose:	support some global function

var contextPath = "."; 
var libraryPath = "/squery/query/htc";

// make a global unique number ID
var g_id = 0;
function g_newid(){
	var date = new Date();
	var id = date.getTime();
	if(g_id < id)
		g_id = id;
	else
		g_id = g_id + 1;

	return g_id;
}

//	Make debug pane
var g_b_trace = true;		// set to false if don't want debug message

function TRACE(msg){	//	global trace function
	if(g_b_trace == false)	return;

	_trace = getTracePane();
	
	if(_trace != null)
		_trace.insertAdjacentHTML("afterBegin","<br>&nbsp;"+msg);
	else
		alert("TRACE: " + msg);
}

var g_trace = null;
var g_trace_init_msg = "<br>&nbsp;<font color='green'>Initialize trace pane completedly, double click pane to erase the trace message.. enjoy it.. 2004.2.2 z</font><br>";
function getTracePane(){	//	get trace pane object, if not exist, then create a new object and return it.
	if(g_trace == null && document.body != null){
		g_trace = document.createElement("div");
		g_trace.style.height = "200";	//	Default trace pane's height
		g_trace.style.width = "100%";	//	Default trace pane's width
		g_trace.style.color = "black";	//	trace pane's font color
		g_trace.style.overflow = "auto";	// set 'visible' disable scroll bar
		g_trace.style.fontFamily = "verdana";
		g_trace.style.fontSize = "12px";
		g_trace.style.lineHeight = "130%";
		g_trace.style.backgroundColor = "silver";
		g_trace.innerHTML = g_trace_init_msg;
		g_trace.ondblclick = eraseTraceMessage;
		document.body.insertAdjacentHTML("beforeEnd","<br><hr>");
		document.body.insertAdjacentElement("beforeEnd",g_trace);
	}

	return g_trace;
}

function eraseTraceMessage(){	//	erase all trace message in trace pane.
	if(confirm("Are you sure to erase all trace message?") == true){
		var _pane = getTracePane();
		_pane.innerHTML = "";
	}
}

//	calculate the element's top and left position

function element_top(el){
	var et = 0;
	while (el)
	{
		et += el.offsetTop;
		el = el.offsetParent;
	}
	return et + parseInt(window.document.body.topMargin);
}

function element_left(el){
	var et = 0;
	while (el)
	{
		et += el.offsetLeft;
		el = el.offsetParent;
	}
	return et + parseInt(window.document.body.leftMargin);
}

//	XML operation support

function LoadXML(xmlurl){
	var xmldoc = new ActiveXObject("MSXML2.DOMDocument");
	xmldoc.async = false;
	xmldoc.load(xmlurl);
  
	if(xmldoc.parseError.errorCode != 0){
		var xmlerr = xmldoc.parseError;
		TRACE("Can not load '" + xmlurl + "', reason: " + xmlerr.reason);
		return null;
	}else
		return xmldoc;
}

function createXMLAttribute(xmlobj,node,attrname,attrvalue){
	var _tmpAttr = xmlobj.createAttribute(attrname);
	_tmpAttr.value = attrvalue;
	node.attributes.setNamedItem(_tmpAttr);
}

function createXMLNode(xmlobj,pnode,nodename,nodetext){
	var _tmpNode = xmlobj.createNode(1,nodename,"");
	if(nodetext != null && nodetext != ""){
		_tmpNode.text = nodetext;
	}
	pnode.appendChild(_tmpNode);
	
	return _tmpNode;
}


//	WMC code

DISPATCHER_LOCATION = "http://FlyingPig/workflow/designer/fn_dispatcher.jsp";		// default dispatcher location

function InvokeHelper(fnID,_xmldoc){
	var httpobj = new ActiveXObject("MSXML2.XMLHTTP");
	var retDom = new ActiveXObject("MSXML2.DOMDocument");
	var retObj = null;
	
	if(_xmldoc == null)	_xmldoc = WMC_MakeBlankDataPackage();

	TRACE("Begin invoking the WMC with fnID = '" + fnID + "'");
	try{
		httpobj.open("post",DISPATCHER_LOCATION + "?fnID=" + fnID,false);
		httpobj.send(_xmldoc);

		retDom.loadXML(httpobj.responseText);
	}catch(e){
		alert("ERROR REPORT: " + e.description);
	}
	TRACE("status is: "+httpobj.status+" '" + httpobj.statusText + "'");
	
	if(httpobj.status != 200){
		retDom = null;
		TRACE("Data Transfer Error!!!");
	}else
		TRACE("Data Transfer Succeeded!!!");
	
	if(retDom != null){
		var docEl = retDom.documentElement;
		if(docEl == null){
			TRACE("Can not analyse the document returned from server...");
			alert("Can not get action result from server. maybe incorrect result.");
			return null;
		}
		
		var fnIdNode = docEl.attributes.getNamedItem("fnID");
		if(fnIdNode == null){
			TRACE("Can not found fnID in root element to analyse the result!");
			alert("Can not find fnID in result object!! incorrect result!");
			return null;
		}
		
		if(fnIdNode.value != fnID){
			TRACE("Different fnID between request and response, request fnID: " + fnID + "; responsefnID: " + fnIdNode.value);
			alert("Server returned  a incorrected result!!!");
			return null;
		}

		var msgNode = docEl.attributes.getNamedItem("ServerMsg");

		var serverMsg;
		if(msgNode != null)	serverMsg = msgNode.value;
		else	serverMsg = "!!!NOT FOUND MESSAGE NODE,MAYBE INCORRECT RESULT!!!";

		TRACE("Server return message: " + serverMsg);
		var msgNode = retDom.documentElement.attributes.getNamedItem("ServerMsg");
		var serverMsg;
		if(msgNode != null)	serverMsg = msgNode.value;
		else	serverMsg = "!!!NOT FOUND MESSAGE NODE!!!";
		
		var newRoot = retDom.selectSingleNode("/WMC-DataPackage/Results/Result[@DataType='Document']");
		
		if(newRoot != null){
			retObj = new ActiveXObject("MSXML2.DOMDocument");
			retObj.loadXML("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
			retObj.appendChild(newRoot.selectSingleNode("Value").firstChild);
		}else{
			newRoot = retDom.selectSingleNode("/WMC-DataPackage/Results/Result[@DataType='String']");
			if(newRoot != null)	retObj = newRoot.selectSingleNode("Value").text;
		}
	}
	
	return retObj;
}

function WMC_MakeBlankDataPackage(){
	var _dom = new ActiveXObject("MSXML2.DOMDocument");
	_dom.loadXML("<?xml version=\"1.0\" encoding=\"GB2312\" ?><WMC-DataPackage><Parameters /><Results /></WMC-DataPackage>");
	return _dom;
}

function WMC_AddDOMParameter(_strDomID,_d,_dom){	//	add a document '_d' to _dom with ID=_strDomID, if _dom was null, then make a blank doc and add parameter to it.
	if(_dom == null){
		_dom = WMC_MakeBlankDataPackage();
	}
	
	if(_strDomID != null){
		var parms = _dom.selectSingleNode("/WMC-DataPackage/Parameters");
		
		var parm = createXMLNode(_dom,parms,"Parameter");
		createXMLAttribute(_dom,parm,"ID",_strDomID);
		createXMLAttribute(_dom,parm,"DataType","Document");
		
		var valNode = createXMLNode(_dom,parm,"Value");
		valNode.appendChild(_d.documentElement);	// make the parameter document's root as value node's content
	}
	
	return _dom;
}

// End of WMC code

function WMC_AddStrParameter(_strID,_strValue,_dom){
	if(_dom == null){	// make a new dom if it was null
		_dom = WMC_MakeBlankDataPackage();
	}
	
	if(_strID != null){
		var parms = _dom.selectSingleNode("/WMC-DataPackage/Parameters");
		
		var parm = createXMLNode(_dom,parms,"Parameter");
		createXMLAttribute(_dom,parm,"ID",_strID);
		createXMLAttribute(_dom,parm,"DataType","String");
		
		createXMLNode(_dom,parm,"Value",_strValue);
	}
	
	return _dom;
}

//	general function library

function getCurrentDate(){
	var date = new Date();
	var s = date.getFullYear() + "-";
	var m = date.getMonth() + 1;
	var d = date.getDate();
	if(m<10)	s = s + "0";
	s = s + m;
	
	s = s + "-";
	if(d<10)	s = s + "0";
	s = s + d;
	
	return s;
}

function AddNamespace(_prefix, _urn){
	if(_prefix == null || _prefix == ""){
		TRACE("Can not adding namespace with given parameter");
		return;
	}
	
	TRACE("Checking '" + _prefix + "' in exist " + document.namespaces.length + " namespace(s)...");
	
	var foundUrn = false;
	for(var i=0;i<document.namespaces.length;i++){
		if(document.namespaces[i].name == _prefix){
			foundUrn = true;
			break;
		}
	}
	
	if(foundUrn == true){
		TRACE("namespace '" + _prefix + "' exist,needn't add");
		return false;
	}
	
	document.namespaces.add(_prefix,_urn);
//	document.xmlns:vml = "urn:schemas-microsoft-com:vml";
}

//var ctlObj = new Object();
//var datatype;// = "datetime"; 
function showCalendar(ctlObj,datatype) {
	var screenX = window.event.screenX + 20;
	var screenY =window.event.screenY + 10;
	var args =new Object;
	args.datatype=datatype;
	args.value =ctlObj.value;
	
	var rst=window.showModalDialog(libraryPath+"/date.html", args, "dialogWidth:230px; dialogHeight:260px; status:0");
	if(rst != null && rst != "null")	ctlObj.value=rst;
}

var ctlObj1 = new Object();
function showCalendar1(ctlObj1,datatype) {
	var screenX = window.event.screenX + 20;
	var screenY =window.event.screenY + 10;
	var args =new Object;
	args.datatype=datatype;
	args.value =ctlObj1.value;
	
	var rst=window.showModalDialog(libraryPath+"/date.htm", args, "dialogWidth:250px; dialogHeight:280px; status:0");
	if(rst)	ctlObj1.value=rst;
}